package insight.miescor.opp.domain;

import org.sormula.Database;
import org.sormula.SormulaException;
import org.sormula.Table;

public class NumberGenerator {
	private String seriesKey;
	private Integer seriesCurrentNum;

	public NumberGenerator() {
	}
	
	public NumberGenerator(String key) {
		seriesKey = key;
		seriesCurrentNum = 1;
	}

	public static NumberGenerator loadNextNum(Database database, String key)
			throws SormulaException {
		Table<NumberGenerator> numGenTable = database
				.getTable(NumberGenerator.class);
		NumberGenerator numGenerator = numGenTable.select(key);
		if (numGenerator != null) {
			numGenerator.setSeriesCurrentNum(numGenerator.seriesCurrentNum + 1);
			numGenTable.update(numGenerator);
		} else {
			numGenerator = new NumberGenerator(key);
			numGenTable.save(numGenerator);
		}
		return numGenerator;
	}

	public Integer getSeriesCurrentNum() {
		return seriesCurrentNum;
	}

	public void setSeriesCurrentNum(Integer seriesCurrentNum) {
		this.seriesCurrentNum = seriesCurrentNum;
	}

	public String getSeriesKey() {
		return seriesKey;
	}

	public void setSeriesKey(String seriesKey) {
		this.seriesKey = seriesKey;
	}
}
